/* 
 * Copyright (C) 2012 UTSChedule
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package utsc.apps.utschedule.ui.activities.adapters;

import java.util.ArrayList;
import java.util.List;

import utsc.apps.utschedule.R;
import utsc.apps.utschedule.classes.Exam;
import utsc.apps.utschedule.util.TimetableHelper;
import android.content.Context;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LayerDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

/**
 * This is a list adapter for {@link  utsc.apps.utschedule.ui.ExamListFragment ExamListFragment}.
 * @author 
 *
 */
public class ExamListAdapter extends ArrayAdapter<Exam> {

	private static final String ADD_TEXT="+ Add new exam";
	private static final String ADD_MARK="Add\nMark";
	private ArrayList<Exam> _exams;
	private int _examType;
	
	public ExamListAdapter(Context context, int textViewResourceId,
			List<Exam> objects, int examType) {
		super(context, textViewResourceId, objects);
		_exams=new ArrayList<Exam>(objects);
		_examType=examType;
	}
	
	public int get_examType() {
		return _examType;
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ExamViewHolder holder;
		if (convertView==null) {
			convertView=LayoutInflater.from(getContext()).inflate(R.layout.exam_list_item, null);
			holder=new ExamViewHolder();
			holder._courseColorView=(TextView) convertView.findViewById(R.id.color_cell_course);
			holder._txtCourseCode=(TextView) convertView.findViewById(R.id.txt_list_course);
			holder._txtDate=(TextView) convertView.findViewById(R.id.txt_list_date);
			holder._txtTime=(TextView)convertView.findViewById(R.id.txt_list_time);
			holder._txtGrade=(TextView) convertView.findViewById(R.id.txt_grade);
			convertView.setTag(holder);
		}else {
			holder=(ExamViewHolder) convertView.getTag();
		}
		Exam exam=getItem(position);
		if (exam!=null) {
			holder._txtCourseCode.setPadding(0, 0, 0, 0);
			if (exam.is_expired()) {
				holder._txtCourseCode.setPaintFlags(Paint.STRIKE_THRU_TEXT_FLAG|Paint.ANTI_ALIAS_FLAG);
				holder._txtCourseCode.setText(exam.toString()+" (Finished)");
				holder._txtGrade.setVisibility(View.VISIBLE);
				//however, we only show the mark if there is a mark already setup
				if (exam.get_mark()>0 && exam.get_total()>0) {
					holder._txtGrade.setText(exam.get_grade().toShortString());
				}else {
					holder._txtGrade.setText(ADD_MARK);
				}
			}else {
				holder._txtCourseCode.setPaintFlags(Paint.ANTI_ALIAS_FLAG);
				holder._txtCourseCode.setText(exam.toString());
				holder._txtGrade.setVisibility(View.INVISIBLE);
			}
			holder._courseColorView.setVisibility(View.VISIBLE);
			holder._txtDate.setVisibility(View.VISIBLE);
			holder._txtDate.setText(TimetableHelper.getFormattedString(exam.get_startTime(), TimetableHelper.TYPE_DATE_WEEKDAY));
			holder._txtTime.setVisibility(View.VISIBLE);
			holder._txtTime.setText(TimetableHelper.getFormattedString(exam.get_startTime(), TimetableHelper.TYPE_TIME)+" - "+TimetableHelper.getFormattedString(exam.get_endTime(),TimetableHelper.TYPE_TIME));
			Drawable colorDrawable=holder._courseColorView.getBackground();
			if (colorDrawable instanceof LayerDrawable) {
				((LayerDrawable) colorDrawable).getDrawable(0).setColorFilter(exam.get_course().get_color(), PorterDuff.Mode.SRC_ATOP);
			}
		}else {
			holder._txtCourseCode.setPadding(0, 15, 0, 15);
			holder._txtCourseCode.setPaintFlags(Paint.ANTI_ALIAS_FLAG);
			holder._txtCourseCode.setText(ADD_TEXT);
			holder._txtTime.setVisibility(View.GONE);
			holder._txtDate.setVisibility(View.GONE);
			holder._courseColorView.setVisibility(View.INVISIBLE);
			holder._txtGrade.setVisibility(View.INVISIBLE);
		}
		return convertView;
		
	}
	
	private static class ExamViewHolder{
		TextView _courseColorView;
		TextView _txtCourseCode;
		TextView _txtTime;
		TextView _txtDate;
		TextView _txtGrade;
	}
	
	@Override
	public int getCount() {
		if (_exams!=null && !_exams.isEmpty()) {
			return _exams.size()+1;
		}
		return 1;
	}
	
	public void set_exams(ArrayList<Exam> _exams) {
		this._exams = _exams;
	}
	
	@Override
	public Exam getItem(int position) {
		if (position<_exams.size()) {
			return _exams.get(position);
		}
		return null;
	}
	
	
}
